//
//  DyShare.m
//  DyShare
//
//  Created by Bell on 14-7-23.
//
//

#import "DyShare.h"
#import <TencentOpenAPI/sdkdef.h>
#import "AFNetworking.h"
#import "UIImage+Format.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>

@implementation DyShare

+ (DyShare *)getInstance
{
    static dispatch_once_t  onceToken;
    static DyShare * __sharedInstance;
    
    dispatch_once(&onceToken, ^{
        __sharedInstance = [[DyShare alloc] init];
    });
    return __sharedInstance;
}

#pragma mark - public

-(void)registerWeiboWithAppId:(NSString *)appId
{
    [WeiboSDK registerApp:appId];
}

-(void)registerWeiXinWithAppId:(NSString *)appId description:(NSString *)description
{
    [WXApi registerApp:appId withDescription:description];
}

-(void)registerQQWithAppId:(NSString *)appId
{
    self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:appId andDelegate:self];
}

-(BOOL)checkWeiXin
{
    return [WXApi isWXAppInstalled];
}

-(BOOL)checkQQ
{
    return [QQApiInterface isQQInstalled];
}

-(BOOL)checkWeiBo
{
    return [WeiboSDK isWeiboAppInstalled];
}

-(NSArray *)getShareArray
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    {
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"微信";
        entity.image = @"dy_share_weixin";
        entity.shareType = DyShareTypeWeiXin;
        [mutableArray addObject:entity];
    }
    {
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"朋友圈";
        entity.image = @"dy_share_pengyouquan";
        entity.shareType = DyShareTypePengYouQuan;
        [mutableArray addObject:entity];
    }
    {
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"新浪微博";
        entity.image = @"dy_share_weibo";
        entity.shareType = DyShareTypeWeiBo;
        [mutableArray addObject:entity];
    }
    {
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"QQ";
        entity.image = @"dy_share_qq";
        entity.shareType = DyShareTypeQQ;
        [mutableArray addObject:entity];
    }
    return mutableArray;
}

-(NSArray *)getLoginArray;
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    {
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"微信";
        entity.image = @"dy_share_weixin";
        entity.loginType = DyLoginTypeWeiXin;
        [mutableArray addObject:entity];
    }
    {
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"新浪微博";
        entity.image = @"dy_share_weibo";
        entity.loginType = DyLoginTypeWeiBo;
        [mutableArray addObject:entity];
    }
    {
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"QQ";
        entity.image = @"dy_share_qq";
        entity.loginType = DyLoginTypeQQ;
        [mutableArray addObject:entity];
    }
    return mutableArray;
}

-(NSArray *)getCanShareArray
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    if ([self checkWeiXin]) {
        {
            DyShareEntity *entity = [[DyShareEntity alloc] init];
            entity.title = @"微信";
            entity.image = @"dy_share_weixin";
            entity.shareType = DyShareTypeWeiXin;
            [mutableArray addObject:entity];
        }
        {
            DyShareEntity *entity = [[DyShareEntity alloc] init];
            entity.title = @"朋友圈";
            entity.image = @"dy_share_pengyouquan";
            entity.shareType = DyShareTypePengYouQuan;
            [mutableArray addObject:entity];
        }
    }
    if([self checkWeiBo]){
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"新浪微博";
        entity.image = @"dy_share_weibo";
        entity.shareType = DyShareTypeWeiBo;
        [mutableArray addObject:entity];
    }
    if ([self checkQQ]) {
        DyShareEntity *entity = [[DyShareEntity alloc] init];
        entity.title = @"QQ";
        entity.image = @"dy_share_qq";
        entity.shareType = DyShareTypeQQ;
        [mutableArray addObject:entity];
    }
    return mutableArray;
}


-(NSArray *)getCanLoginArray
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    if ([self checkWeiXin]) {
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"微信";
        entity.image = @"dy_share_weixin";
        entity.loginType = DyLoginTypeWeiXin;
        [mutableArray addObject:entity];
    }
    if([self checkWeiBo]){
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"新浪微博";
        entity.image = @"dy_share_weibo";
        entity.loginType = DyLoginTypeWeiBo;
        [mutableArray addObject:entity];
    }
    if ([self checkQQ]) {
        DyLoginEntity *entity = [[DyLoginEntity alloc] init];
        entity.title = @"QQ";
        entity.image = @"dy_share_qq";
        entity.loginType = DyLoginTypeQQ;
        [mutableArray addObject:entity];
    }
    return mutableArray;
}

-(void)loginQQWithdelegate:(id<DyShareDelegate>)delegate
{
    if (![self checkQQ]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法登录" message:NSLocalizedString(@"请安装\"QQ\"后再登录", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return;
    }
    self.delegate = delegate;
    self.login = YES;
    self.loginType = DyLoginTypeQQ;
    NSArray * permissions = [NSArray arrayWithObjects:
                             kOPEN_PERMISSION_ADD_ALBUM,
                             kOPEN_PERMISSION_GET_USER_INFO,
                             kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
                             kOPEN_PERMISSION_ADD_ALBUM,
                             kOPEN_PERMISSION_ADD_ONE_BLOG,
                             kOPEN_PERMISSION_ADD_SHARE,
                             kOPEN_PERMISSION_ADD_TOPIC,
                             kOPEN_PERMISSION_CHECK_PAGE_FANS,
                             kOPEN_PERMISSION_GET_INFO,
                             kOPEN_PERMISSION_GET_OTHER_INFO,
                             kOPEN_PERMISSION_LIST_ALBUM,
                             kOPEN_PERMISSION_UPLOAD_PIC,
                             kOPEN_PERMISSION_GET_VIP_INFO,
                             kOPEN_PERMISSION_GET_VIP_RICH_INFO,
                             nil];
    [_tencentOAuth authorize:permissions];
}

-(void)loginWeiXinWithdelegate:(id<DyShareDelegate>)delegate
{
    if (![self checkWeiXin]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法登录" message:NSLocalizedString(@"请安装\"微信\"后再登录", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return;
    }
    self.delegate = delegate;
    self.login = YES;
    self.loginType = DyLoginTypeWeiXin;
    SendAuthReq* req =[[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"123" ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}

-(void)loginWeiBoWithdelegate:(id<DyShareDelegate>)delegate
{
//    if (![self checkWeiBo]) {
//        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法登录" message:NSLocalizedString(@"请安装\"微博\"后再登录", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
//        [alerView show];
//        return;
//    }
    self.delegate = delegate;
    self.login = YES;
    self.loginType = DyLoginTypeWeiBo;
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = @"http://www.zallhy.com/";
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController"};
    [WeiboSDK sendRequest:request];
}

- (BOOL)shareWithShareType:(DyShareType)shareType Content:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (shareType == DyShareTypeQQ) {
        return [self shareQQWithContent:content title:title url:url image:image delegate:delegate];
    } else if (shareType == DyShareTypeWeiBo) {
        //微博特殊处理
        return [self shareWeiBoWithContent:title title:title url:url image:image delegate:delegate];
    } else if (shareType == DyShareTypeWeiXin) {
        return [self shareWeiXinWithContent:content title:title url:url image:image delegate:delegate];
    } else if (shareType == DyShareTypePengYouQuan) {
        return [self sharePengYouQuanWithContent:content title:title url:url image:image delegate:delegate];
    }
    else if(shareType == DyShareTypeQZone)
    {
        return [self shareQZoneWithContent:content title:title url:url image:image delegate:delegate];
    }
    return NO;
}

- (BOOL)shareQQWithContent:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (![self checkQQ]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"QQ\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeQQ;
    
    NSData *data = [image dataWithMaxLength:1 * 1024 * 1024];
    
    // 0 < 标题长度必须 < 128
    title = title.length == 0 ? @" " : (title.length > 128 ? [title substringToIndex:127] : title);
    // 内容长度必须 < 512
    content = content.length > 512 ? [content substringToIndex:511] : content;
    QQApiNewsObject *new = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:title description:content previewImageData:data];
    
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:new];
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    return sent == EQQAPISENDSUCESS;
}

- (BOOL)shareQQWithImage:(NSString *)title description:(NSString *)description image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate {
    if (![self checkQQ]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"QQ\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeQQ;
    
    //开发者分享图片数据
    NSData *imgData = UIImagePNGRepresentation(image);
    QQApiImageObject *imgObj = [QQApiImageObject objectWithData:imgData
                                               previewImageData:imgData
                                                          title:title
                                                   description :description];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
    //将内容分享到qq
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    return sent == EQQAPISENDSUCESS;
}

- (BOOL)shareQZoneWithImage:(NSString *)title description:(NSString *)description image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate {
    if (![self checkQQ]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"QQ\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeQQ;
    
    NSData *imgData = UIImagePNGRepresentation(image);
    NSArray *imgarray = [NSArray arrayWithObject:imgData];
    QQApiImageArrayForQZoneObject *qzoneimg = [QQApiImageArrayForQZoneObject objectWithimageDataArray:imgarray title:title];
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:qzoneimg];
    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    [self handleSendResult:sent];
//    //开发者分享图片数据
//    NSData *imgData = UIImagePNGRepresentation(image);
//    QQApiImageObject *imgObj = [QQApiImageObject objectWithData:imgData
//                                               previewImageData:imgData
//                                                          title:title
//                                                   description :description];
//    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
//    //将内容分享到qq
//    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    
    return sent == EQQAPISENDSUCESS;
}

- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQZONENOTSUPPORTTEXT:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"空间分享不支持QQApiTextObject，请使用QQApiImageArrayForQZoneObject分享" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIQZONENOTSUPPORTIMAGE:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"空间分享不支持QQApiImageObject，请使用QQApiImageArrayForQZoneObject分享" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        case EQQAPIVERSIONNEEDUPDATE:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"当前QQ版本太低，需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        default:
        {
            break;
        }
    }
}


- (BOOL)shareQZoneWithContent:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (![self checkQQ]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"QQ\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeQZone;
    
    NSData *data = [image dataWithMaxLength:1 * 1024 * 1024];
    
    // 0 < 标题长度必须 < 128
    title = title.length == 0 ? @" " : (title.length > 128 ? [title substringToIndex:127] : title);
    // 内容长度必须 < 512
    content = content.length > 512 ? [content substringToIndex:511] : content;
    QQApiNewsObject *new = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:title description:content previewImageData:data];
    SendMessageToQQReq* req = [SendMessageToQQReq reqWithContent:new];
    QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    return sent == EQQAPISENDSUCESS;
}



- (BOOL)shareWeiBoWithContent:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (![self checkWeiBo]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微博\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeWeiBo;
    
    NSInteger maxLength = 140;
    content = (content.length + url.length + 1 ) > maxLength ? [content substringToIndex:(maxLength - url.length - 2)] :content;
    
    WBMessageObject *message = [WBMessageObject message];
    NSString *messagetitle = [NSString stringWithFormat:@"%@ %@",content,url];
    message.text = messagetitle;
    WBImageObject *imageObject = [WBImageObject object];
    
    NSData *data = [image dataWithMaxLength:10 * 1024 * 1024];
    imageObject.imageData = data;
    if (imageObject.imageData) {
        message.imageObject = imageObject;
    }
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};

    return [WeiboSDK sendRequest:request];
}

- (BOOL)shareWeiBoWithImage:(NSString *)text image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate {
    if (![self checkWeiBo]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微博\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeWeiBo;
    
    WBMessageObject *message = [WBMessageObject message];
    message.text = text;
    
    WBImageObject *imageObject = [WBImageObject object];
    imageObject.imageData = UIImagePNGRepresentation(image);;
    message.imageObject = imageObject;
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message];
    
    return [WeiboSDK sendRequest:request];
}

- (BOOL)shareWeiXinWithContent:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (![self checkWeiXin]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微信\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeWeiXin;
    title = title.length > 512 ? [title substringToIndex:512] : title;
    content = content.length > 1024 ? [content substringToIndex:1023] : content;
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = title;
    message.description = content;
    
    [message setThumbImage:[image imageWithMaxLength:16 * 1024]];
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl =url;
    message.mediaObject = ext;

    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.scene = WXSceneSession;
    req.bText = NO;
    req.message = message;
    return [WXApi sendReq:req];
}

- (BOOL)shareWeiXinWithImage:(UIImage *)image delegate:(id<DyShareDelegate>)delegate {
    if (![self checkWeiXin]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微信\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeWeiXin;
    
    WXMediaMessage *message = [WXMediaMessage message];
    
    NSData *source = UIImagePNGRepresentation(image);
    NSData *newImageData = UIImageJPEGRepresentation([UIImage imageWithData:source scale:0.1], 0.1f);
    UIImage *smallimage = [UIImage imageWithData:newImageData];
    
    // 压缩图片分辨率(因为data压缩到一定程度后，如果图片分辨率不缩小的话还是不行)
    CGSize newSize = CGSizeMake(200, 200);
    UIGraphicsBeginImageContext(newSize);
    [smallimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [message setThumbImage:newImage];
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = UIImagePNGRepresentation(image);
    message.mediaObject = imageObject;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.scene = WXSceneSession;
    req.bText = NO;
    req.message = message;
    return [WXApi sendReq:req];
}

- (BOOL)shareWeiXinPYQWithImage:(UIImage *)image delegate:(id<DyShareDelegate>)delegate {
    if (![self checkWeiXin]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微信\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypeWeiXin;
    
    WXMediaMessage *message = [WXMediaMessage message];
    
    NSData *source = UIImagePNGRepresentation(image);
    NSData *newImageData = UIImageJPEGRepresentation([UIImage imageWithData:source scale:0.1], 0.1f);
    UIImage *smallimage = [UIImage imageWithData:newImageData];
    
    // 压缩图片分辨率(因为data压缩到一定程度后，如果图片分辨率不缩小的话还是不行)
    CGSize newSize = CGSizeMake(200, 200);
    UIGraphicsBeginImageContext(newSize);
    [smallimage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [message setThumbImage:newImage];
    WXImageObject *imageObject = [WXImageObject object];
    imageObject.imageData = UIImagePNGRepresentation(image);
    message.mediaObject = imageObject;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.scene = WXSceneTimeline;
    req.bText = NO;
    req.message = message;
    return [WXApi sendReq:req];
}

- (BOOL)sharePengYouQuanWithContent:(NSString *)content title:(NSString *)title url:(NSString *)url image:(UIImage *)image delegate:(id<DyShareDelegate>)delegate
{
    if (![self checkWeiXin]) {
        UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"无法分享" message:NSLocalizedString(@"请安装\"微信\"后再分享", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:NSLocalizedString(@"确定", nil), nil];
        [alerView show];
        return NO;
    }
    self.delegate = delegate;
    self.login = NO;
    self.shareType = DyShareTypePengYouQuan;
    WXMediaMessage *message = [WXMediaMessage message];
    
    // 朋友圈分享只会显示title
    
    NSString *newTitle = [NSString stringWithFormat:@"%@\n%@",title,content];
    newTitle = newTitle.length > 512 ? [newTitle substringToIndex:512] : newTitle;
    message.title = newTitle;
    [message setThumbImage:[image imageWithMaxLength:16 * 1024]];
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = url;
    
    message.mediaObject = ext;
 
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    BOOL result = [WXApi sendReq:req];
    return result;
}

#pragma mark - WXApiDelegate
#pragma mark - TencentSessionDelegate - QQApiInterfaceDelegate

-(void)onReq:(BaseReq*)req{
    NSLog(@"onReq");
}

/*! @brief 发送一个sendReq后，收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp具体的回应内容，是自动释放的
 */




-(void)onResp:(id)resp
{
    if (_login) { // 登陆
        if (_loginType == DyLoginTypeWeiXin) { // 微信登陆
            SendAuthResp *sendAuthResp  = (SendAuthResp *)resp;
            if (sendAuthResp.errCode == 0) {
                NSLog(@"weixin login success");
                if ([self.delegate respondsToSelector:@selector(didWeiXinLoginSuccess:sender:)]) {
                    [self.delegate didWeiXinLoginSuccess:sendAuthResp sender:self];
                }
            }
        }
    } else { // 分享
        if (_shareType == DyShareTypeQQ) { // QQ分享
            QQBaseResp *qqResp = resp;
            if ([qqResp.result intValue] == 0) {
                // qq分享成功
                NSLog(@"QQ share success");
                if ([self.delegate respondsToSelector:@selector(didQQShareSuccess:sender:)]) {
                    [self.delegate didQQShareSuccess:qqResp sender:self];
                }
            }
        } else if (_shareType == DyShareTypeWeiXin) { // 微信好友分享
            BaseResp *wxResp = resp;
            if (wxResp.errCode == 0) {
                // 微信分享成功;
                NSLog(@"weixin share success");
                if ([self.delegate respondsToSelector:@selector(didWeiXinShareSuccess:sender:)]) {
                    [self.delegate didWeiXinShareSuccess:wxResp sender:self];
                }
            }
        } else if (_shareType==DyShareTypePengYouQuan) { // 朋友圈分享
            BaseResp *wxResp = resp;
            if (wxResp.errCode == 0) {
                // 朋友圈分享成功;
                NSLog(@"pengyouquan share success");
                if ([self.delegate respondsToSelector:@selector(didPengYouQuanShareSuccess:sender:)]) {
                    [self.delegate didPengYouQuanShareSuccess:wxResp sender:self];
                }
            }
        }
    }
}

#pragma mark -WeiboSDKDelegate

-(void)didReceiveWeiboRequest:(WBBaseRequest *)request{
    
}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
    if ([response isKindOfClass:[WBSendMessageToWeiboResponse class]]) { // 微博分享
        WBSendMessageToWeiboResponse *sendMessageToWeiboResponse = (WBSendMessageToWeiboResponse*)response;
        if (sendMessageToWeiboResponse.statusCode == 0) {
            NSLog(@"weibo share success");
            if ([self.delegate respondsToSelector:@selector(didWeiBoShareSuccess:sender:)]) {
                [self.delegate didWeiBoShareSuccess:sendMessageToWeiboResponse sender:self];
            }
        }
    } else if ([response isKindOfClass:[WBAuthorizeResponse class]]) { // 微博登陆
        NSLog(@"weibo login success");
        WBAuthorizeResponse *authorizeResponse = (WBAuthorizeResponse*)response;
        if ([self.delegate respondsToSelector:@selector(didWeiBoLoginSuccess:sender:)]) {
            [self.delegate didWeiBoLoginSuccess:authorizeResponse sender:self];
        }
    }
}

#pragma mark - TencentSessionDelegate - TencentLoginDelegate(授权登录回调协议)

- (void)tencentDidLogin
{
    if ([_tencentOAuth accessToken]) { // QQ登陆
        NSLog(@"QQ login success");
        if ([self.delegate respondsToSelector:@selector(didQQLoginSuccess:sender:)]) {
            [self.delegate didQQLoginSuccess:_tencentOAuth sender:self];
        }
    }
}

/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录
 */
- (void)tencentDidNotLogin:(BOOL)cancelled
{
    
}

/**
 * 登录时网络有问题的回调
 */
- (void)tencentDidNotNetWork
{
    
}

#pragma mark - TencentSessionDelegate - QQApiInterfaceDelegate

/**
 处理QQ在线状态的回调
 */
- (void)isOnlineResponse:(NSDictionary *)response
{
    
}

#pragma mark - TencentSessionDelegate - TencentWebViewDelegate
#pragma mark - TencentSessionDelegate - TencentApiInterfaceDelegate



@end
